package dyyx;

import java.time.LocalDateTime;

import com.alibaba.dubbo.common.Constants;
import com.alibaba.dubbo.common.extension.Activate;
import com.alibaba.dubbo.rpc.Filter;
import com.alibaba.dubbo.rpc.Invocation;
import com.alibaba.dubbo.rpc.Invoker;
import com.alibaba.dubbo.rpc.Result;
import com.alibaba.dubbo.rpc.RpcContext;
import com.alibaba.dubbo.rpc.RpcException;

@Activate(group = {Constants.PROVIDER, Constants.CONSUMER})
public class DubboHelloFilter implements Filter{
	
	public DubboHelloFilter() {
		System.out.println("DubboHelloFilter() run "+LocalDateTime.now()+"\n\n\n");
	}

	public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
        
		String traceId = RpcContext.getContext().getAttachment("traceId");
		// before filter ...
		long start = System.currentTimeMillis();
        Result result = invoker.invoke(invocation);
		String traceId2 = RpcContext.getContext().getAttachment("traceId");

		long end = System.currentTimeMillis();
		long time = end - start;
		System.out.println("DubboHelloFilter time="+time+","+traceId+","+traceId2+"\n\n\n");

        // after filter ...
        return result;
    }

}
